//
//  StrategyDateParameter.m
//  StrategyInspector
//
//  Created by jiro on 12/07/29.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "StrategyDateParameter.h"

StrategyDateParameter::StrategyDateParameter()
{
	type = StrategyParameter::Type_Date;
	currentDate = NULL;
	startDate = NULL;
	endDate = NULL;
	interval = 0;
}

StrategyDateParameter::StrategyDateParameter( NSDate* date )
{
	type = StrategyParameter::Type_Date;
	currentDate = date;
	startDate = date;
	endDate = date;
	interval = 0;
}

StrategyDateParameter::StrategyDateParameter( NSDate* startDateArg, NSDate* endDateArg, double intervalArg )
{
	type = StrategyParameter::Type_Date;
	currentDate = startDateArg;
	startDate = startDateArg;
	endDate = endDateArg;
	interval = intervalArg;
}

bool StrategyDateParameter::isMutable() const
{
	if ( interval == 0 )
	{
		return true;
	}
	return false;
}

void StrategyDateParameter::nextValue( bool& isLast )
{
	currentDate = [ currentDate dateByAddingTimeInterval: interval ];
	if ( [ endDate timeIntervalSinceDate: currentDate ] <= 0 )
	{
		isLast = true;
	}
	else
	{
		isLast = false;
	}
}

void StrategyDateParameter::initialize()
{
	currentDate = startDate;
}

NSDate* StrategyDateParameter::getDateValue() const
{
	return currentDate;
}

NSDate* StrategyDateParameter::getDateValueStart() const
{
	return startDate;
}

NSDate* StrategyDateParameter::getDateValueEnd() const
{
	return endDate; 
}

void StrategyDateParameter::setDateValue( NSDate* date )
{
	currentDate = date;
}

StrategyParameter* StrategyDateParameter::copy() const
{
	StrategyDateParameter* p = new StrategyDateParameter( startDate, endDate, interval );
	return p;
}

void StrategyDateParameter::setValueMiddle()
{
	double middle = [ endDate timeIntervalSinceDate: startDate ] / 2.0;
	currentDate = [ startDate dateByAddingTimeInterval: middle ];
}
